package com.example.mycamerademo.cameraxlearing

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.Camera
import android.os.Build
import android.view.Surface


/**
 * @author tgw
 * @date 2021/12/3
 * @describe
 */
class CameraUtils {

    companion object {
        /***
         * 是否存在前置摄像头
         * @return true or false
         */
        fun hasFrontCamera(context: Context): Boolean {
            return context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
        }


        private fun checkCameraFacing(facing: Int): Boolean {
            if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
                return false
            }
            val cameraCount: Int = Camera.getNumberOfCameras()
            val info = Camera.CameraInfo()
            for (i in 0 until cameraCount) {
                Camera.getCameraInfo(i, info)
                if (facing == info.facing) {
                    return true
                }
            }
            return false
        }

        fun hasBackFacingCamera(): Boolean {
            return checkCameraFacing(1)
        }

        fun hasFrontFacingCamera(): Boolean {
            return checkCameraFacing(0)
        }

        fun getSdkVersion(): Int {
            return Build.VERSION.SDK_INT
        }


        fun getRotation(context: Activity){
            context.windowManager.defaultDisplay.getRotation()
        }

//        private fun getCameraOri(rotation: Int): Int {
//            var degrees = rotation * 90
//            when (rotation) {
//                Surface.ROTATION_0 -> degrees = 0
//                Surface.ROTATION_90 -> degrees = 90
//                Surface.ROTATION_180 -> degrees = 180
//                Surface.ROTATION_270 -> degrees = 270
//                else -> {
//                }
//            }
//            var result: Int
//            val info = Camera.CameraInfo()
//            Camera.getCameraInfo(mCameraId, info)
//            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
//                result = (info.orientation + degrees) % 360
//                result = (360 - result) % 360
//            } else {
//                result = (info.orientation - degrees + 360) % 360
//            }
//            return result
//        }
    }

}